<script setup>
import { ref, onMounted, computed } from 'vue';
import { Search, ArrowDown, Upload, Bell, User, More, ArrowRight, Document, Star, Setting } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';
import Header from '../components/Header.vue';
import { getCoursePage } from '@/api/course'
import { getCommunityArticles } from '@/api/community'
import { useUserStore } from '../stores/user'

const router = useRouter();
const userStore = useUserStore();

// 检查用户是否登录
const isLoggedIn = computed(() => userStore.isLoggedIn);
const userInfo = computed(() => userStore.userInfo);

// 轮播图数据
const carouselImages = ref([
  { 
    url: '/images/Carousel1.jpg',
    title: '探索编程世界',
    tag: '热门课程'
  },
  { 
    url: '/images/Carousel2.jpg',
    title: '数据科学入门',
    tag: '推荐'
  },
  { 
    url: '/images/Carousel3.png',
    title: '前端开发实战',
    tag: '新课'
  },
  { 
    url: '/images/Carousel5.jpg',
    title: 'AI技术应用',
    tag: '热门'
  }
]);

// 课程卡片数据
const courseList = ref([])

onMounted(async () => {
  // 获取课程数据，最多8个
  const res = await getCoursePage({ page: 1, pageSize: 8 })
  if ((res.code === 0 || res.code === 200) && res.data && Array.isArray(res.data.records)) {
    courseList.value = res.data.records
  }
})

// 文章列表数据

const articleList = ref([])

onMounted(async () => {
  // ...课程数据...

  
  // 获取社区文章数据，最多6个
  const articleRes = await getCommunityArticles({ page: 1, pageSize: 6 })
  if ((articleRes.code === 0 || articleRes.code === 200) && articleRes.data && Array.isArray(articleRes.data.records)) {
    articleList.value = articleRes.data.records
  }
})


// 当前激活的菜单
const activeMenu = ref('首页');

// 显示搜索框
const showSearch = ref(false);
const toggleSearch = () => {
  showSearch.value = !showSearch.value;
};

// 显示导航更多菜单
const showMoreMenu = ref(false);
const toggleMoreMenu = () => {
  showMoreMenu.value = !showMoreMenu.value;
};

// 显示上传菜单
const showUploadMenu = ref(false);
const toggleUploadMenu = () => {
  showUploadMenu.value = !showUploadMenu.value;
};

// 点击个人卡片按钮
const goToMyArticles = () => {
  router.push('/my-articles');
};

const goToMyCollections = () => {
  router.push('/my-collect');
};

const goToUserCenter = () => {
  router.push('/profile');
};

// 跳转到登录页面
const goToLogin = () => {
  router.push('/login');
};


</script>

<template>
  <Header />
  <div class="website-container">
    <!-- 智慧学习平台标题 -->
    <div class="platform-title">
      <h1>智慧学习平台</h1>
    </div>
    
    <!-- 渐变装饰包裹层 -->
    <div class="section-container banner-section">
      <div class="banner-layout">
        <!-- 轮播图 -->
    <div class="banner-container">
          <el-carousel height="360px" :interval="5000" arrow="hover" indicator-position="none">
        <el-carousel-item v-for="(item, index) in carouselImages" :key="index">
              <div class="carousel-slide">
                <div class="carousel-image">
            <el-image :src="item.url" fit="cover"></el-image>
                </div>
                <div class="carousel-caption">
                  <span class="carousel-tag">{{ item.tag }}</span>
                  <h2>{{ item.title }}</h2>
                </div>
          </div>
        </el-carousel-item>
      </el-carousel>
          <div class="carousel-indicators">
            <span 
              v-for="(item, index) in carouselImages" 
              :key="index"
              class="indicator-dot"
              :class="{ active: index === 0 }"
              @click="$refs.carousel?.setActiveItem(index)"
            ></span>
          </div>
        </div>

        <!-- 用户信息卡片 -->
        <div class="user-info-card">
          <div v-if="isLoggedIn" class="logged-in-container">
            <div class="user-profile-header">
              <el-avatar :size="60" :src="userInfo.avatar">
                <el-icon><User /></el-icon>
              </el-avatar>
              <div class="user-name">{{ userInfo.username || '用户' }}</div>
            </div>
            
            <div class="inspirational-quote">
              <p>学如逆水行舟，不进则退</p>
              <span class="quote-source">-《增广贤文》</span>
            </div>
            
            <div class="user-actions">
              <div class="action-box" @click="goToMyArticles">
                <el-icon><Document /></el-icon>
                <span>我的文章</span>
              </div>
              <div class="action-box" @click="goToMyCollections">
                <el-icon><Star /></el-icon>
                <span>我的收藏</span>
              </div>
              <div class="action-box" @click="goToUserCenter">
                <el-icon><Setting /></el-icon>
                <span>个人中心</span>
              </div>
            </div>
          </div>
          <div v-else class="not-logged-in">
            <el-avatar :size="60">
              <el-icon><User /></el-icon>
            </el-avatar>
            <p>登录后查看您的个人信息</p>
            <el-button type="primary" @click="goToLogin">立即登录</el-button>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 课程列表 -->
    <div class="section-container">
      <div class="section-header">
        <h2 class="section-title">首页推荐</h2>
        <el-button class="more-btn" type="text" @click="router.push('/learning')">更多</el-button>
      </div>
      
      <div class="course-list">
        <el-row :gutter="32">
          <el-col
            v-for="course in courseList"
            :key="course.courseId"
            :xs="24"
            :sm="12"
            :md="12"
            :lg="6"
          >
            <div class="square-course-card" @click="router.push(`/course/${course.courseId}`)">
              <div class="square-course-cover">
                <img :src="course.courseCover" :alt="course.courseName" />
              </div>
              <div class="square-course-info">
                <h3 class="square-course-title">{{ course.courseName }}</h3>
                <div class="square-course-category">{{ course.courseClassify }}</div>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    
    <!-- 文章列表 -->
    <div class="section-container">
      <div class="section-header">
        <h2 class="section-title">推荐文章</h2>
        <el-button class="more-btn" type="text" @click="router.push('/community')">更多</el-button>
      </div>
      <div class="article-list-horizontal">
        <div
                      v-for="article in articleList.slice(0, 6)"
          :key="article.id"
        class="article-horizontal-card"
        @click="router.push(`/community/article/${article.id}`)"
        >
          <div class="article-h-left">
            <el-avatar :src="article.creatorAvatar" size="large" />
            <div class="author-info">
              <div class="author-name">{{ article.createUser || '匿名' }}</div>
            </div>
          </div>
          <div class="article-h-center">
            <div class="article-h-title">{{ article.title }}</div>
            <div class="article-h-summary">{{ article.summary }}</div>
            <div class="article-h-meta">
              <span class="meta-item">阅读 {{ article.viewCount || 0 }}</span>
              <span class="meta-item">赞 {{ article.likeCount || 0 }}</span>
              <span class="meta-item">收藏 {{ article.collectCount || 0 }}</span>
            </div>
          </div>
          <div class="article-h-right">
            <div v-if="article.tag || article.category" class="article-tag-block">
              {{ article.tag || article.category || '标签' }}
        </div>
            <el-image
              v-else
              class="article-h-cover"
              :src="article.coverImage || 'https://via.placeholder.com/80x80/3d8aff/ffffff?text=LOGO'"
              fit="cover"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.website-container {
  width: 100%;
  margin: 0 auto;
  padding: 0 20px 32px 20px;
  background: #fff;
  min-height: 100vh;
  font-family: 'Inter', 'Source Sans Pro', 'Microsoft YaHei', sans-serif;
}

.header {
  margin-left: -20px;
  margin-right: -20px;
  width: calc(100% + 40px);
}

.nav-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

.nav-left,
.nav-right {
  display: flex;
  align-items: center;
}

.logo {
  margin-right: 20px;
}

.main-nav {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
}

.main-nav li {
  padding: 0 15px;
  font-size: 16px;
  cursor: pointer;
  position: relative;
  line-height: 60px;
}

.main-nav li.active,
.main-nav li:hover {
  color: #409eff;
}

.more-menu {
  position: relative;
}

.dropdown-menu {
  position: absolute;
  top: 60px;
  left: 0;
  width: 120px;
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  padding: 10px 0;
  z-index: 10;
}

.dropdown-menu ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.dropdown-menu li {
  padding: 8px 16px;
  line-height: normal;
}

.dropdown-menu li:hover {
  background-color: #f5f7fa;
}

.nav-right {
  display: flex;
  align-items: center;
}

.nav-right > * {
  margin-left: 15px;
}

.search-btn,
.upload-btn {
  position: relative;
}

.search-dropdown,
.upload-dropdown {
  position: absolute;
  top: 50px;
  right: 0;
  min-width: 200px;
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  padding: 10px;
  z-index: 10;
}

.upload-dropdown ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.upload-dropdown li {
  padding: 8px 16px;
  cursor: pointer;
}

.upload-dropdown li:hover {
  background-color: #f5f7fa;
}

.user-info {
  margin-left: 20px;
  font-size: 14px;
}

/* 轮播图样式 */
.banner-section {
  padding-top: 0;
  margin-top: 20px;
}

.banner-layout {
  display: flex;
  gap: 20px;
  width: 100%;
  margin-bottom: 20px;
}

.banner-container {
  flex: 1;
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.carousel-slide {
  position: relative;
  width: 100%;
  height: 100%;
}

.carousel-image {
  width: 100%;
  height: 100%;
}

.carousel-image .el-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 1s ease;
}

.carousel-slide:hover .carousel-image .el-image {
  transform: scale(1.03);
}

.carousel-caption {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(to top, rgba(0,0,0,0.6) 0%, rgba(0,0,0,0) 100%);
  padding: 80px 40px 30px;
  color: white;
  text-align: left;
}

.carousel-tag {
  display: inline-block;
  background-color: #2c5dff;
  color: white;
  font-size: 14px;
  font-weight: 500;
  padding: 6px 12px;
  border-radius: 4px;
  margin-bottom: 10px;
}

.carousel-caption h2 {
  font-size: 32px;
  font-weight: 600;
  margin: 0;
  letter-spacing: 0.5px;
  text-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

.carousel-indicators {
  position: absolute;
  bottom: 20px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 8px;
  z-index: 10;
}

.indicator-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: rgba(255,255,255,0.5);
  cursor: pointer;
  transition: all 0.3s ease;
}

.indicator-dot.active, .indicator-dot:hover {
  background-color: white;
  transform: scale(1.2);
}

/* 自定义轮播图箭头样式 */
:deep(.el-carousel__arrow) {
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  opacity: 0;
  transition: all 0.3s;
}

.banner-container:hover :deep(.el-carousel__arrow) {
  opacity: 1;
}

:deep(.el-carousel__arrow:hover) {
  background-color: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
}

:deep(.el-carousel__arrow i) {
  font-size: 20px;
}

/* 课程列表样式 */
.section-container {
  margin: 30px 0 20px 0;
  width: 100%;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 10px;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
  position: relative;
  padding-left: 12px;
}

.section-title::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 16px;
  background: #2c5dff;
  border-radius: 2px;
}

.section-title.right {
  margin-left: auto;
}

.course-list {
  margin-bottom: 30px;
}

.course-item-col {
  margin-bottom: 20px;
}

/* 调整为两行四列大卡片风格 */
.square-course-card {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  margin-bottom: 20px;
  transition: transform 0.3s, box-shadow 0.3s;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  height: 240px;
  border: 1px solid #eee;
}
.square-course-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(44, 93, 255, 0.15);
}
.square-course-cover {
  width: 100%;
  height: 0;
  padding-bottom: 56%;
  position: relative;
  background: #f5f5f5;
}
.square-course-cover img {
  position: absolute;
  left: 0; top: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-bottom: 2px solid #f0f3ff;
}
.square-course-info {
  flex: 1;
  padding: 20px 18px 16px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}
.square-course-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.square-course-category {
  font-size: 14px;
  color: #6b7280;
  margin-top: auto;
  display: flex;
  align-items: center;
}

.square-course-category::before {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  background: #2c5dff;
  border-radius: 50%;
  margin-right: 8px;
}

/* 文章列表样式 */
.article-list-horizontal {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 30px;
}
.article-horizontal-card {
  display: flex;
  align-items: center;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  padding: 16px 20px;
  transition: all 0.25s ease;
  cursor: pointer;
  border: 1px solid #eee;
}
.article-horizontal-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(44, 93, 255, 0.1);
}
.article-h-left {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 60px;
  margin-right: 24px;
}
.author-info {
  margin-top: 6px;
  text-align: center;
}
.author-name {
  font-size: 14px;
  color: #2c5dff;
  font-weight: 500;
}
.article-h-center {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.article-h-title {
  font-size: 19px;
  font-weight: 600;
  color: #333;
  margin-bottom: 10px;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.article-h-summary {
  color: #6b7280;
  font-size: 15px;
  margin-bottom: 12px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}
.article-h-meta {
  font-size: 13px;
  color: #94a3b8;
  display: flex;
  gap: 18px;
}
.meta-item {
  display: inline-flex;
  align-items: center;
}
.meta-item::before {
  content: "";
  display: inline-block;
  width: 4px;
  height: 4px;
  background: #94a3b8;
  border-radius: 50%;
  margin-right: 6px;
}
.article-h-right {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 180px;
  margin-left: 24px;
}
.article-tag-block {
  background: #2c5dff;
  color: #fff;
  border-radius: 4px;
  padding: 6px 10px;
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  min-width: 60px;
}
.article-h-cover {
  width: 160px;
  height: 90px;
  border-radius: 12px;
  object-fit: cover;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.07);
  transition: transform 0.2s, box-shadow 0.2s;
}
.article-h-cover:hover {
  transform: scale(1.04);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}

/* 热招职位和热门课程样式 */
.section-row {
  display: flex;
  gap: 20px;
}

.section-col {
  flex: 1;
  background-color: #f7f7f7;
  padding: 20px;
  border-radius: 4px;
}

.more-link {
  margin-left: auto;
  color: #409eff;
  cursor: pointer;
  font-size: 14px;
}

@media screen and (max-width: 900px) {
  .banner-layout {
    flex-direction: column;
  }
  
  .user-info-card {
    width: 100%;
    height: auto;
    padding: 20px;
  }
  
  .user-actions {
    flex-direction: row;
    gap: 10px;
  }
  .banner-container {
    max-width: 100%;
    border-radius: 15px;
  }
  .section-container {
    margin: 30px 0 24px 0;
  }
  .article-horizontal-card {
    flex-direction: column;
    align-items: flex-start;
    padding: 20px 16px;
  }
  .article-h-left {
    flex-direction: row;
    width: auto;
    margin-right: 12px;
  }
  .article-h-right {
    margin-left: 0;
    margin-top: 16px;
    width: 100%;
  }
  .article-h-cover {
    width: 100%;
    height: 120px;
  }
}

.more-btn {
  color: #2c5dff;
  font-size: 14px;
  font-weight: 400;
  padding: 6px 12px;
  border-radius: 4px;
  transition: all 0.2s;
  background: rgba(44, 93, 255, 0.05);
}
.more-btn:hover {
  background: rgba(44, 93, 255, 0.1);
}

/* 用户信息卡片样式 */
.user-info-card {
  width: 320px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 20px;
  display: flex;
  flex-direction: column;
  border: 1px solid #eee;
  height: 360px;
}

.logged-in-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.user-profile-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 15px;
}

.user-name {
  margin-top: 12px;
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.inspirational-quote {
  text-align: center;
  margin: 5px 0 auto;
  padding: 10px;
  position: relative;
}

.inspirational-quote p {
  font-size: 16px;
  color: #2c5dff;
  font-weight: 500;
  font-style: italic;
  line-height: 1.5;
  margin-bottom: 5px;
}

.quote-source {
  display: block;
  text-align: right;
  font-size: 13px;
  color: #6b7280;
  font-style: italic;
  margin-top: 3px;
  padding-right: 10px;
}

.user-actions {
  display: flex;
  flex-direction: row;
  gap: 10px;
  margin-top: 15px;
}

.action-box {
  flex: 1;
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  background: #f5f7fa;
  border-radius: 8px;
  padding: 12px;
  transition: all 0.3s;
  cursor: pointer;
}

.action-box:hover {
  background: rgba(44, 93, 255, 0.1);
  transform: translateY(-2px);
}

.action-box .el-icon {
  font-size: 24px;
  color: #2c5dff;
  margin-bottom: 8px;
}

.action-box span {
  font-size: 14px;
  color: #333;
}

.not-logged-in {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 20px;
}

.not-logged-in p {
  margin: 0;
  color: #6b7280;
}

/* 平台标题样式 */
.platform-title {
  text-align: center;
  padding: 15px 0;
  margin-bottom: 10px;
}

.platform-title h1 {
  font-size: 28px;
  font-weight: 600;
  color: #2c5dff;
  margin: 0;
  letter-spacing: 2px;
  background: linear-gradient(90deg, #2c5dff, #0097ff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
</style>
